# -*- coding: utf-8 -*-
#
# Author:: Jonny
# Date:: 2022/4/25
import base64
import json

import requests


def base64_api(uname, pwd, img, typeid):
    with open(img, "rb") as f:
        base64_data = base64.b64encode(f.read())
        b64 = base64_data.decode()
    data = {"username": uname, "password": pwd, "typeid": typeid, "image": b64}
    result = json.loads(requests.post("http://api.ttshitu.com/predict", json=data).text)
    if result["success"]:
        return result["data"]["result"]
    else:
        return result["message"]
    return ""


def get_verify_code_by_tujian_ocr(img_path):
    # typeid: 1-纯数字  1001-纯数字2
    res = base64_api(uname="17744555659", pwd="Admin123", img=img_path, typeid=1001)
    return res


if __name__ == "__main__":

    def __test_base64_api():
        img_path = """D:\screenshot-20220425-023441.png"""
        result = base64_api(uname="17744555659", pwd="Admin123", img=img_path, typeid=3)
        print(result)

    def __test_get_verify_code():
        img_path = """D:\screenshot-20220425-023441.png"""
        res = get_verify_code(img_path)
        print(res)

    __test_get_verify_code()
